package com.fromfuture.xuhuidoc.cordova.plugin;

import android.content.Intent;
import android.util.Log;
import android.view.ViewGroup;

import com.fromfuture.xuhuidoc.android.callback.BlActivityToMyConsultRoomFra02Callbak;
import com.fromfuture.xuhuidoc.android.callback.Doc_close_Video_Callback;
import com.fromfuture.xuhuidoc.android.callback.OtherPatExitCallBack;
import com.fromfuture.xuhuidoc.android.fragments.MyConsultRoomFragment02;
import com.fromfuture.xuhuidoc.android.utils.MySharedPreferencesUtils;
import com.fromfuture.xuhuidoc.cordova.untils.CordovaAppManager;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Author：何送军
 * creation time：2017/9/4
 **/
public class MyConsultRoomPlugin extends CordovaPlugin {
    private CallbackContext callbackContext;
    private String tag="MyConsultRoomPlugin ";
    private String HASPATIN="haspatin";
    private String RECEIVEPATMSG="receive_pat_msg";
    private String OPENVIDEO="openvideo";
    private String CLOSEVIDEO="closevideo";
    private String DOCINFO="docinfo";
    private String OTHER_PAT_EXIT="other_pat_exit";

    private static int patposs=1000;

    // 广播action
    private final String ORDERED_ACTION = "com.fromfuture.xuhuidoc.haspatin";
    private final String RECEIVEPATMSG_ACTION ="com.fromfuture.xuhuidoc.receivepatmsg";

    //外部接口
    static Doc_close_Video_Callback doc_close_video_callback;
    static OtherPatExitCallBack otherPatExitCallBack;
    static BlActivityToMyConsultRoomFra02Callbak blActivityToMyConsultRoomFra02Callbak;
    /**
     * set方法和get方法 开始
     * */
    public static int getPatpos() {
        return patposs;
    }

    public static void setPatpos(int patpos) {
        patposs = patpos;
    }
    public void setDoc_close_video_callback(Doc_close_Video_Callback doc_close_video_callback) {
        this.doc_close_video_callback = doc_close_video_callback;
    }
    public  void setOtherPatExitCallBack(OtherPatExitCallBack otherPatExitCallBack) {
        this.otherPatExitCallBack = otherPatExitCallBack;
    }

    public  void setBlActivityToMyConsultRoomFra02Callbak(BlActivityToMyConsultRoomFra02Callbak blActivityToMyConsultRoomFra02Callbak) {
        this.blActivityToMyConsultRoomFra02Callbak = blActivityToMyConsultRoomFra02Callbak;
    }

    /**
     * set方法和get方法 结束
     * */
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals(HASPATIN)){
            this.callbackContext=callbackContext;
            JSONObject jsonObject=args.getJSONObject(0);
            Log.d(tag,jsonObject.toString());
            Intent intent=new Intent();
            intent.setAction(ORDERED_ACTION);
            intent.putExtra("haspatin",jsonObject.toString());
            CordovaAppManager.myConsultRoomActivity.sendBroadcast(intent);
        }else if (action.equals(RECEIVEPATMSG)){
            this.callbackContext=callbackContext;
            JSONObject jsonObject=args.getJSONObject(0);
            Log.d(tag,jsonObject.toString());
            Intent intent=new Intent();
            intent.setAction(RECEIVEPATMSG_ACTION);
            intent.putExtra("patmsg",jsonObject.toString());
            CordovaAppManager.myConsultRoomActivity.sendBroadcast(intent);
        }else if (action.equals(OPENVIDEO)){
            this.callbackContext=callbackContext;
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d("answer need video","哈哈哈");
                    MyConsultRoomFragment02.doc_open_video();
                }
            });

        }else if (action.equals(CLOSEVIDEO)){
            this.callbackContext=callbackContext;
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    docCloseVideo01();
                }
            });
        }else if (action.equals(DOCINFO)){
            Log.d("医生信息啊","手动");
            this.callbackContext=callbackContext;
            String phone,name,guid,jobtitle,morentx,tx,pwd;
            phone= MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCPHONE","");
            name= MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCNAME","");
            guid= MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCGUID","");
            jobtitle=MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCJOBTITLE","");
            morentx=MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCMORETX","");
            tx=MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCTX","");
            pwd=MySharedPreferencesUtils.getValue(cordova.getActivity(),"DOCINFO","DOCPWD","");
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("phone",phone);
            jsonObject.put("name",name);
            jsonObject.put("guid",guid);
            jsonObject.put("jobtitle",jobtitle);
            jsonObject.put("morentx",morentx);
            jsonObject.put("tx",tx);
            jsonObject.put("pwd",pwd);
            callbackContext.success(jsonObject);
        }else if (action.equals(OTHER_PAT_EXIT)){
            this.callbackContext=callbackContext;
            JSONObject jsonObject=args.getJSONObject(0);
            String other_pat_exit_guid=jsonObject.getString("other_pat_guid");
            String other_pat_name=jsonObject.getString("other_pat_name");
            otherPatExitCallBack.other_pat_exit(other_pat_exit_guid,other_pat_name);
        }
        return false;

    }


    public static void my_jiezhen(final String patguid,int pos){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "Jiezhen(\'" + patguid+"" + "\')");
            }
        });
        setPatpos(pos);
    }

    //把医生信息发给诊室

    //医生发送文字
    public static void docsendMessageText01(String patguid,String msg){

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("patguid",patguid);
            jsonObject.put("msg",msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final String arr=jsonObject.toString();
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "docSendMessageText(\'" +arr + "\')");
            }
        });
    }
    //医生发送图片
    public static void docsendMessagePhoto(final String fileName){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "docSendMessagePhoto(\'" + fileName + "\')");
            }
        });
    }
    //医生成功发送病例
    public static void docsendBl(final String blguid){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "docSendBl(\'" + blguid + "\')");
            }
        });
        blActivityToMyConsultRoomFra02Callbak.sendblguid(blguid);
    }
    //医生打开视频
    public static void my_openvideo(final String patguid,final String docguid){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "docOpenVideo(\'" + patguid+","+docguid + "\')");
            }
        });
    }

    //医生打开语音聊天
    public static void my_openaudio(final String patguid,final String docguid){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "docOpenAudio(\'" + patguid+","+docguid + "\')");
            }
        });
    }
    //医生关闭视频
    public static void docCloseVideo(){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "docCloseVideo(\'" +"" + "\')");
                ((ViewGroup) CordovaAppManager.mycordovaWebView.getView().getParent()).removeView(CordovaAppManager.mycordovaWebView.getView());
                doc_close_video_callback.closevideo("doc_close_video",getPatpos());
            }
        });
    }

    //患者退出诊室
    private void docCloseVideo01(){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    ((ViewGroup) CordovaAppManager.mycordovaWebView.getView().getParent()).removeView(CordovaAppManager.mycordovaWebView.getView());
                }catch (Exception e){

                }
                doc_close_video_callback.closevideo("pat_exit_room",getPatpos());
            }
        });

    }
    //医生点击结束一个病人，主要是让病人收到医生退出消息
    public static void docCloseOnePat(final String patguid){
        CordovaAppManager.myConsultRoomActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CordovaAppManager.mycordovaWebView.loadUrl("javascript:"+ "Jiezhen(\'" + patguid+"" + "\')");
            }
        });
    }

}
